package com.e3.template.tgf;

import java.util.List;

import com.e3.template.model.TGFTreeModel;
import com.e3.web.core.client.TreeListener;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyMenu;
import com.e3.web.widget.client.MyMenuItem;
import com.e3.web.widget.model.MyTreeModel;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.google.gwt.user.client.Element;

public abstract class MyTGFUI<D extends TGFTreeModel> extends SuperGFUI<D> implements TreeListener<D> {
	
	private ContentPanel contentPanel = new ContentPanel(new BorderLayout());
	private TGFTreePanel<D> treePanel;
	private D treeSelectedModel;
	private Object actionSource;
	
	public MyTGFUI(){
		TGFStateMachine.init(getCurrentLocalName());
	}
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		this.setLayout(new FitLayout());
		
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.WEST);
		bld.setSplit(true);
		bld.setMargins(new Margins(0,2,0,0));
		contentPanel.add(getTreePanel(),bld);
		
		bld = new BorderLayoutData(LayoutRegion.CENTER);
		contentPanel.add(getRightContentPanel(),bld);
		
		getRightContentPanel().setHeaderVisible(false);
		contentPanel.setHeaderVisible(false);
		
		bld = new BorderLayoutData(LayoutRegion.NORTH);
		bld.setSplit(true);
		bld.setMargins(new Margins(0,2,0,0));
		getRightContentPanel().add(getGridPanel(),bld);
		bld = new BorderLayoutData(LayoutRegion.CENTER);
		getRightContentPanel().add(getFormPanel(),bld);
		getFormPanel().setFormReadonly(true);
		this.add(contentPanel);
		
		initToolBar();
		contentPanel.setTopComponent(getToolbar());

	}
	
	public TGFTreePanel<D> getTreePanel() {
		if(treePanel == null){
			treePanel = createTreePanel();
			treePanel.addTreeListener(this);
			treePanel.setTreeContextMenu(getTreeContextMenu());
			treePanel.setMaskComponent(this);		
		}
		return treePanel;
	}
	
	protected Menu getTreeContextMenu(){
		return getTreeGridContextMenu();
	}
	
	protected Menu getGridContextMenu(){
		return getTreeGridContextMenu();
	}
	
	protected Menu getTreeGridContextMenu(){
		MyMenu menu = new MyMenu();
		
		BaseAction[] actions = getTreeGridContextMenuActions();
		for (BaseAction baseAction : actions) {
			MyMenuItem item = new MyMenuItem(baseAction);
			item.addSelectionListener(getMnuSelectListener());
			menu.add(item);
		}
		
		return menu;

	}
	
	protected BaseAction[] getTreeGridContextMenuActions(){
		return new BaseAction[]{
				TGFStateMachine.NEW_CHILD_ACTION,TGFStateMachine.NEW_ACTION
				,TGFStateMachine.MODIFY_ACTION,TGFStateMachine.DELETE_ACTION}; 
	}
	
	protected void onBtnMnuEvent(BaseAction action) {
		D selectedModel = getCurrentModel(action);
		actionSource = action.getSource();
		super.onBtnMnuEvent(action);
		if(action == TGFStateMachine.NEW_CHILD_ACTION){
			onNewChild(selectedModel);
		}
	}
	
	protected Object getActionSource() {
		return actionSource;
	}

	protected List<D> getCurrentSelectedModels(){
		List<D> models = getGridPanel().getSelectedModels();
		if(getActionSource()!=null||getActionSource() instanceof TGFTreePanelAsync){
			models = getTreePanel().getChildren(treeSelectedModel, true);
			if(!isTreeRootNode(treeSelectedModel)){
				if(treeSelectedModel!=null){
					models.add(0,treeSelectedModel);
				}
			}
		}
		return models;
	}
	
	protected boolean isTreeRootNode(D model){
		return getTreePanel().isRootNode(model);
	}
	
	public void onEditSuccess(D model) {
		getGridPanel().refreshGridModel(model);
		getTreePanel().refreshTreeModel(model);
	}
	
	protected void onDeleteSuccess(List<D> models){
		getGridPanel().removeValues(models);
		getTreePanel().removeTreeNodes(models);
	}

	protected D getCurrentModel(BaseAction action){
		Object obj = action.getSource();
		D selectedModel = getGridSelectedModel();
		if(obj instanceof TGFTreePanelAsync){
			selectedModel = treeSelectedModel;
		}
		return selectedModel;
	}

	
	public void onTreeDataSelected(D parentModel, List<D> subModels) {
		treeSelectedModel = parentModel;
		getGridPanel().setGridValues(subModels);
	}
	
	protected void onNewChild(D selectedModel) {
		D newModel = createModelInstance();
		((MyTreeModel)newModel).setParentModel(selectedModel);
		getEditorDialog().showDialog(newModel,getCurrentLocalName(),true);
	}

	protected abstract TGFTreePanelAsync<D> createTreePanel();
}
